package it.sandler.android.database;

import it.sandler.android.database.types.SandlerBool;
import it.sandler.android.database.types.SandlerFloat;
import it.sandler.android.database.types.SandlerInteger;
import it.sandler.android.database.types.SandlerLong;
import it.sandler.android.database.types.SandlerFieldRelation;
import it.sandler.android.database.types.SandlerText;

import java.lang.reflect.Field;

/**
 * You can do what ever you want to do with SandlerDB.<br>
 * There are no restrictions. You can use SandlerDB for commercial use.<br>
 * <br>
 * Circle me on Google+: http://gplus.to/roberthahn<br>
 * SandlerDB on Google Code: http://code.google.com/p/sandler-db-for-android/<br>
 * <br>
 * Date: 04/16/2012
 * 
 * @author Robert Hahn
 * 
 */
public abstract class SandlerEntity extends SandlerBaseType {

	private boolean changed;

	public SandlerEntity() {
		// make sure all class fields have instances
		Class<? extends SandlerEntity> classOf = this.getClass();
		for (Field f : classOf.getDeclaredFields()) {
			f.setAccessible(true);
			try {
				if (f.get(this) == null) {
					String type = f.getType().getSimpleName();
					if (type.equals(SandlerBool.class.getSimpleName()))
						f.set(this,
								SandlerType.create(SandlerBool.class, false));
					else if (type.equals(SandlerInteger.class.getSimpleName()))
						f.set(this, SandlerType.create(SandlerInteger.class, 0));
					else if (type.equals(SandlerText.class.getSimpleName()))
						f.set(this, SandlerType.create(SandlerText.class, null));
					else if (type.equals(SandlerLong.class.getSimpleName()))
						f.set(this, SandlerType.create(SandlerLong.class, 0L));
					else if (type.equals(SandlerFloat.class.getSimpleName()))
						f.set(this, SandlerType.create(SandlerFloat.class, 0f));
					else if (type.equals(SandlerFieldRelation.class.getSimpleName()))
						f.set(this, SandlerFieldRelation.class.newInstance());
					if (type.equals(SandlerList.class.getSimpleName()))
						f.set(this, new SandlerList<SandlerEntity>(this));

				}
			} catch (Exception e) {
			}
			f.setAccessible(false);
		}

	}

	public void set(SandlerLong sandler, long value) {
		setChanged(true);
		sandler.set(value);
	}

	public void set(SandlerBool sandler, boolean value) {
		setChanged(true);
		sandler.set(value);
	}

	public void set(SandlerText sandler, String value) {
		setChanged(true);
		sandler.set(value);
	}

	public void set(SandlerFloat sandler, float value) {
		setChanged(true);
		sandler.set(value);
	}

	public void set(SandlerInteger sandler, int value) {
		setChanged(true);
		sandler.set(value);
	}

	public <T extends SandlerEntity> void set(SandlerFieldRelation<T> old, T value) {
		old.set(value);
	}

	public <T extends SandlerEntity> T get(SandlerFieldRelation<T> sandler) {
		return sandler.get();
	}

	public Long get(SandlerLong sandler) {
		return sandler.get();
	}

	public Boolean get(SandlerBool sandler) {
		return sandler.get();
	}

	public String get(SandlerText sandler) {
		return sandler.get();
	}

	public Float get(SandlerFloat sandler) {
		return sandler.get();
	}

	public Integer get(SandlerInteger sandler) {
		return sandler.get();
	}

	private long _id = 0;

	public final long getId() {
		return _id;
	}

	protected final void setId(long id) {
		_id = id;
		setChanged(true);
	}

	protected boolean changed() {
		return changed || getId() == 0;
	}

	protected void setChanged(boolean changed) {
		this.changed = changed;
	}

}
